﻿//-----------------------------------------------------------------------
// <copyright file="PivotTableRow.cs" company="Forebrain Technology, LLC">
//
// Copyright (c) Forebrain Technology, LLC. All rights reserved.
// 
// This source code is released under the Microsoft Public License (Ms-PL).
//
// </copyright>
//-----------------------------------------------------------------------
namespace PivotCollectionTools.Data
{
    using System;
    using System.Collections;
    using System.Collections.ObjectModel;
    using System.Linq;

    /// <summary>
    /// A data row in the pivot table.
    /// </summary>
    [Serializable]
    public class PivotTableRow : ICloneable
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="PivotTableRow"/> class.
        /// </summary>
        public PivotTableRow()
        {
            this.Cells = new PivotTableCellCollection();
        }

        /// <summary>
        /// Gets the cells for the row.
        /// </summary>
        /// <value>The cells of the pivot table row.</value>
        public PivotTableCellCollection Cells { get; private set; }

        /// <summary>
        /// Gets the value of items as strings.
        /// </summary>
        /// <value>The items of the pivot table row.</value>
        public Collection<string> Items
        {
            get
            {
                var itemCollection = new Collection<string>();
                foreach (var cell in this.Cells)
                {
                    itemCollection.Add(cell.Value);
                }

                return itemCollection;
            }
        }

        /// <summary>
        /// Gets the value of items as strings.
        /// </summary>
        /// <value>The items of the pivot table row.</value>
        public Collection<string> DataBoundItems
        {
            get
            {
                var itemCollection = new Collection<string>();
                foreach (var cell in this.Cells.Where(c => c.Column.Category.DataBound))
                {
                    itemCollection.Add(cell.Value);
                }

                return itemCollection;
            }
        }

        /// <summary>
        /// Gets the <see cref="PivotCollectionTools.Data.PivotTableCell"/> with the specified column name.
        /// </summary>
        /// <param name="columnName">The column name to get.</param>
        /// <value>A <see cref="PivotTableCell"/> with the specified column name.</value>
        public PivotTableCell this[string columnName]
        {
            get
            {
                return this.Cells[columnName];
            }
        }

        /// <summary>
        /// Clones this instance.
        /// </summary>
        /// <returns>A copy if this instance.</returns>
        public PivotTableRow Clone()
        {
            PivotTableRow newRow = (PivotTableRow)this.MemberwiseClone();
            return newRow;
        }

        #region ICloneable Members

        /// <summary>
        /// Creates a new object that is a copy of the current instance.
        /// </summary>
        /// <returns>
        /// A new object that is a copy of this instance.
        /// </returns>
        object ICloneable.Clone()
        {
            return this.Clone();
        }

        #endregion
    }
}
